layui.define(['table', 'form'], function(exports){
  var $ = layui.$,table = layui.table,form = layui.form;

    var activityId = $('input[name="activityId"]').val();

  table.render({
    elem: '#LAY-table'
    ,url: layui.setter.webRoot + '/place/physical_store/list.data'
    ,method: 'post'
    ,parseData: function (res) {
        return {
            "code": res.code,
            "msg": res.msg,
            "count": res.body.total,
            "data": res.body.data
        };
    }
    ,response: {statusCode: 200}
    ,size:'lg'
    ,cols: [[
      {type: 'checkbox', fixed: 'left'}
      ,{field: 'id', title: '网点ID'}
      ,{field: 'thumbnailImg', title: '图片', templet: function (record){
          if(record.thumbnailImg) {
              return '<img src="'+record.thumbnailImg+'"  style="max-height:100%;margin:auto;display:block;"/>';
          }else{
              return '<img src="'+layui.setter.webRoot + '/layuiadmin/style/res/default01.png" style="max-height:100%;margin:auto;display:block;"/>';
          }
      }}
      ,{field: 'name', title: '网点名称'}
      ,{field: 'typeNames', title: '网点类型'}
      ,{field: 'userName', title: '所属代理'}
      ,{field: 'deviceFunNames', title: '设备功能项', templet: function (record){
        if(record.deviceFunNames){
            return record.deviceFunNames;
        }
        return '无';
      }}
      ,{field: 'deviceCount', title: '设备数量', templet: function (record){
          if(record.deviceCount){
              return record.deviceCount;
          }
          return '0';
      }}
      ,{field: 'areaName', title: '所在区域'}
      ,{field: 'stateName', title:'状态'}
      ,{field: 'createDate', title: '创建时间'}
    ]]
    ,page: true
    ,limit: 30
    ,height: 'full-220'
    ,text: '对不起，加载出现异常！'
  });
  
  //监听搜索
  form.on('submit(LAY-search)', function(data){
    var field = data.field;
    //执行重载
    table.reload('LAY-table', {
      where: field
      ,page: {curr: 1}
    });
  });
  
  //事件
  var active = {
    add: function(){
        var checkStatus = table.checkStatus('LAY-table')
            , data = checkStatus.data;
        var ids = '';
        for (var i = 0; i < data.length; i++) {
            if (i == 0) ids += data[i].id;
            else ids = ids + ',' + data[i].id;
        }
        if (ids == '' || data.length < 1) {
            layer.msg("至少选择一个网点", {icon: 3});
            return;
        }

        layer.confirm('确定要设置这'+data.length+'个网点吗？', function(index){
            $.ajax({
                type: "post",
                url: layui.setter.webRoot + '/business/coupon_activity/place/add.do',
                data:{activityId:activityId,ids:ids},
                dataType:"json",
                success:function(res){
                    if(res.code == 200){
                        table.reload('LAY-table');
                        layer.msg('设置成功',{icon: 1});
                    }else{
                        layer.msg(res.msg,{icon: 5});
                    }
                }
            });
            layer.close(index);
        });
    }
  };
  
  $('.layui-btn.LAY-btn-fun').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
  
  table.on('tool(LAY-table)', function(obj){
  	var type = obj.event ;
   	active[type] ? active[type].call(this,obj) : '';
  });

  exports('coupon_activity_place_list', {})
});